const fs = require('fs');
const path = require('path');

function resolve(dir) {
  return path.resolve(__dirname, dir);
}
const join = path.join;
function getEntries(path) {
  let files = fs.readdirSync(resolve(path));
  const entries = files.reduce((ret, item) => {
    const itemPath = join(path, item);
    const isDir = fs.statSync(itemPath).isDirectory();
    if (isDir) {
      ret[item] = resolve(join(itemPath, 'index.ts'));
    } else {
      const [name] = item.split('.');
      ret[name] = resolve(`${itemPath}`);
    }
    return ret;
  }, {});
  return entries;
}

const antdvTheme = {
  less: {
    lessOptions: {
      modifyVars: {
        'link-color': '#266FE8', // 链接色
        'link-hover-color': '#266FE8', // 链接色
        'font-size-base': '14px', // 主字号
        'heading-color': 'rgba(0, 0, 0, 0.85)', // 标题色
        'text-color': 'rgba(0, 0, 0, 0.65)', // 主文本色
        'text-color-secondary': 'rgba(0, 0, 0, 0.45)', // 次文本色
        'disabled-color': 'rgba(0, 0, 0, 0.25)', // 失效色
        'border-radius-base': '4px', // 组件/浮层圆角
        'border-color-base': '#D6DBE3', // 边框色
        'box-shadow-base': '0 2px 8px rgba(0, 0, 0, 0.15)', // 浮层阴影
        'height-base': '28px', // 默认控件高度
        'btn-padding-horizontal-base': '10px' // 按钮左右padding距离
      },
      javascriptEnabled: true
    }
  }
};

//开发环境配置
const devConfig = {
  css: {
    loaderOptions: antdvTheme
  },
  transpileDependencies: [
    'chart.js',
    'date-fns',
    'vue-chartkick',
    'chartkick',
    'chartjs-adapter-date-fns'
  ],
  pages: {
    index: {
      entry: 'examples/main.ts',
      template: 'public/index.html',
      filename: 'index.html'
    }
  },
  configureWebpack: {
    resolve: {
      extensions: ['.js', '.vue', '.json'],
      alias: {
        assets: resolve('@/examples/assets'),
        views: resolve('@/examples/views')
      }
    },
    module: {
      rules: [
        {
          test: /\.js$/,
          include: [path.resolve(__dirname, 'node_modules/chart.js')],
          use: 'babel-loader'
        }
      ]
    }
  },
  chainWebpack: (config) => {
    config.module
      .rule('js')
      .include.add('/packages')
      .end()
      .use('babel')
      .loader('babel-loader')
      .tap((options) => {
        return options;
      });
  },
  devServer: {
    port: 8090,
    hot: true,
    proxy: require('./proxyconfig.json')
  }
};
//生成环境配置
const buildConfig = {
  css: {
    sourceMap: true,
    extract: {
      filename: 'style/[name].css'
    },
    loaderOptions: antdvTheme
  },
  configureWebpack: {
    entry: {
      ...getEntries('packages')
    },
    output: {
      filename: '[name]/index.js',
      libraryTarget: 'commonjs2'
    },
    externals: ['vue'],
    //打包模式 production 压缩/development 不压缩, 正式发布版本删除此字段，测试版本为了方便查看错误没启用压缩
    mode: 'production',
    module: {
      rules: [
        {
          test: /\.js$/,
          include: [path.resolve(__dirname, 'node_modules/chart.js')],
          use: 'babel-loader'
        }
      ]
    }
  },
  chainWebpack: (config) => {
    config.module
      .rule('js')
      .include.add('/packages')
      .end()
      .use('babel')
      .loader('babel-loader')
      .tap((options) => {
        return options;
      });
    config.optimization.delete('splitChunks');
    config.plugins.delete('copy');
    config.plugins.delete('html');
    config.plugins.delete('preload');
    config.plugins.delete('prefetch');
    config.plugins.delete('hmr');
    config.entryPoints.delete('app');

    config.module
      .rule('fonts')
      .use('url-loader')
      .tap((option) => {
        option.fallback.options.name = 'static/fonts/[name].[hash:8].[ext]';
        return option;
      });
  },
  outputDir: 'lib',
  productionSourceMap: false
};

module.exports = process.env.NODE_ENV === 'development' ? devConfig : buildConfig;
